package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.mine.entity.OrderUserAddressDataBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderUserAddressDefaultDataBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderUserAddressDeleteDataBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/4/10.
 * 用户地址信息请求
 */

public class OrderAddressManageModel implements OrderAddressManageModelImpl {

    private OrderAddressManageModel.OrderUserAddressDataResponseListener mListener;
    private OrderAddressManageModel.OrderUserAddressDefaultDataResponseListener mListenerDefault;
    private OrderAddressManageModel.OrderUserAddressDeleteDataResponseListener mListenerDelete;

    private OrderUserAddressDataBean.DataBean.AddressBean mChangeBean;
    private OrderUserAddressDataBean.DataBean.AddressBean mChangeAfterBean;
    private static final String TAG = "OrderAddressManageModel";

    public OrderAddressManageModel(OrderAddressManageModel.OrderUserAddressDataResponseListener listener,
                                   OrderAddressManageModel.OrderUserAddressDefaultDataResponseListener listenerDefault,
                                   OrderAddressManageModel.OrderUserAddressDeleteDataResponseListener listenerDelete) {
        mListener = listener;
        mListenerDefault = listenerDefault;
        mListenerDelete = listenerDelete;
    }

    public interface OrderUserAddressDataResponseListener {
        void onGetDetailSuccess(OrderUserAddressDataBean bean);

        void onGetDetailError(Throwable throwable);

    }

    public interface OrderUserAddressDefaultDataResponseListener {
        void onGetDetailSuccessDefault(OrderUserAddressDefaultDataBean bean,OrderUserAddressDataBean.DataBean.AddressBean changeBean,
                                       OrderUserAddressDataBean.DataBean.AddressBean changeAfterBean);

        void onGetDetailErrorDefault(Throwable throwable);

    }

    public interface OrderUserAddressDeleteDataResponseListener {
        void onDeleteSuccess(OrderUserAddressDeleteDataBean bean, int position);

        void onDeleteError(Throwable throwable);
    }

    //请求用户地址信息
    @Override
    public void getUserAddress(String user_id) {
        {
            RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("user_id", user_id)
                    .build();
            Observable<OrderUserAddressDataBean> observable = DataManager.getOrderUserAddressData(body);
            Observable<OrderUserAddressDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
            observeOn.subscribe(new DisposableObserver<OrderUserAddressDataBean>() {
                @Override
                public void onNext(OrderUserAddressDataBean bean) {
                    if (mListener != null) {
                        mListener.onGetDetailSuccess(bean);
                    }
                }

                @Override
                public void onError(Throwable e) {
                    if (mListener != null) {
                        LogUtils.e(e.getMessage(), TAG);
                        mListener.onGetDetailError(e);
                    }
                }

                @Override
                public void onComplete() {

                }
            });
        }

    }

    /**
     * 修改用户默认收货地址返回值
     *
     * @param user_id    用户id
     * @param address_id 地址 id
     */
    @Override
    public void getUserAddressDef(String user_id, String address_id,OrderUserAddressDataBean.DataBean.AddressBean changeBean,
                                  OrderUserAddressDataBean.DataBean.AddressBean changeAfterBean) {
        mChangeBean=changeBean;
        mChangeAfterBean=changeAfterBean;
        {
            RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("user_id", user_id)
                    .addFormDataPart("address_id", address_id)
                    .build();
            Observable<OrderUserAddressDefaultDataBean> observable = DataManager.getOrderUserAddressDefaultData(body);
            Observable<OrderUserAddressDefaultDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
            observeOn.subscribe(new DisposableObserver<OrderUserAddressDefaultDataBean>() {
                @Override
                public void onNext(OrderUserAddressDefaultDataBean bean) {
                    if (mListenerDefault != null) {
                        mListenerDefault.onGetDetailSuccessDefault(bean,mChangeBean,mChangeAfterBean);
                    }
                }

                @Override
                public void onError(Throwable e) {
                    if (mListenerDefault != null) {
                        LogUtils.e(e.getMessage(), TAG);
                        mListenerDefault.onGetDetailErrorDefault(e);
                    }
                }

                @Override
                public void onComplete() {

                }
            });
        }
    }

    /**
     * 删除用户地址，需要给出用户id
     *
     * @param address_id 用户id
     * @param position
     */
    @Override
    public void getUserAddressDel(String address_id, final int position) {
        {
            RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("address_id", address_id)
                    .build();
            Observable<OrderUserAddressDeleteDataBean> observable = DataManager.getOrderUserAddressDeleteData(body);
            Observable<OrderUserAddressDeleteDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
            observeOn.subscribe(new DisposableObserver<OrderUserAddressDeleteDataBean>() {
                @Override
                public void onNext(OrderUserAddressDeleteDataBean bean) {
                    if (mListenerDelete != null) {
                        mListenerDelete.onDeleteSuccess(bean, position);
                    }
                }
                @Override
                public void onError(Throwable e) {
                    if (mListenerDelete != null) {
                        LogUtils.e(e.getMessage(), TAG);
                        mListenerDelete.onDeleteError(e);
                    }
                }
                @Override
                public void onComplete() {

                }
            });
        }
    }
}
